﻿using System;
using System.Configuration;

namespace EPiBoost.Core.Helpers
{
	public static class ConfigurationHelper
	{

		public static string GetString(string appSettingKey, string defaultValue = null)
		{
			var stringVal = ConfigurationManager.AppSettings[appSettingKey];
			if (stringVal != null)
			{
				return stringVal;
			}
			return defaultValue;
		}


		public static bool GetBool(string appSettingKey, bool defaultValue = false)
		{
			var stringVal = GetString(appSettingKey);
			if (stringVal != null)
			{
				bool result;
				if (bool.TryParse(stringVal, out result))
				{
					return result;
				}
			}
			return defaultValue;
		}


		public static DateTime GetDateTime(string appSettingKey)
		{
			return GetDateTime(appSettingKey, DateTime.MinValue);
		}

		public static DateTime GetDateTime(string appSettingKey, DateTime defaultValue)
		{
			var stringVal = GetString(appSettingKey);
			if (stringVal != null)
			{
				DateTime result;
				if (DateTime.TryParse(stringVal, out result))
				{
					return result;
				}
			}
			return defaultValue;
		}


		public static int GetInt(string appSettingKey, int defaultValue = 0)
		{
			var stringVal = GetString(appSettingKey);
			if (stringVal != null)
			{
				int result;
				if (int.TryParse(stringVal, out result))
				{
					return result;
				}
			}
			return defaultValue;
		}

		public static long GetLong(string appSettingKey, long defaultValue = 0)
		{
			var stringVal = GetString(appSettingKey);
			if (stringVal != null)
			{
				long result;
				if (long.TryParse(stringVal, out result))
				{
					return result;
				}
			}
			return defaultValue;
		}

		public static TimeSpan GetTimeSpan(string appSettingKey)
		{
			return GetTimeSpan(appSettingKey, TimeSpan.MinValue);
		}

		public static TimeSpan GetTimeSpan(string appSettingKey, TimeSpan defaultValue)
		{
			var stringVal = GetString(appSettingKey);
			if (stringVal != null)
			{
				TimeSpan result;
				if (TimeSpan.TryParse(stringVal, out result))
				{
					return result;
				}
			}
			return defaultValue;
		}

	}
}
